#!/bin/sh

# Slackware build script for Ice

# Written by Aleksandar Samardzic <asamardzic@gmail.com>

PRGNAM=Ice
VERSION=${VERSION:-3.3.0}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# Now the fun begins.  Ice build system consists of plain makefiles,
# that doesn't seem as supposed to be easily configurable, and some of
# which are plain buggy in some aspects.  So we'll first have to patch
# a bit.

# First, select programming languages which Ice support will be built
# for.  The C++ support will be always enabled, and the default
# selection for remaining languages supported by Ice consists of
# Python, Ruby and PHP here, because Ice could be built for these on
# vanilla Slackware installation.
ICE_LANGUAGES="py rb php"

# Patch top-level makefile with above selection.
sed -i -r \
    -e "s/^SUBDIRS\s+=.*/SUBDIRS = cpp $ICE_LANGUAGES/" \
    -e "s/^CLEAN_SUBDIRS\s+=.*/CLEAN_SUBDIRS = cpp $ICE_LANGUAGES/" \
    -e "s/^DEPEND_SUBDIRS\s+=.*/DEPEND_SUBDIRS = cpp $ICE_LANGUAGES/" \
    -e "s/^INSTALL_SUBDIRS\s+=.*/INSTALL_SUBDIRS = cpp $ICE_LANGUAGES/" \
    Makefile

# Now, replace every $(CFLAGS) reference in Ice makefiles to $(CFLAGS)
# $(EXTRA_CFLAGS), in order to be able to pass $SLKCFLAGS to the
# compiler at "make" phase below; similarly, replace every $(CXXFLAGS)
# reference in Ice makefiles to $(CXXFLAGS) $(EXTRA_CXXFLAGS).
for file in $(grep -ril -E '\$\(CFLAGS\)' ./*); do
    sed 's/$(CFLAGS)/$(CFLAGS) $(EXTRA_CFLAGS)/' -i $file;
done
for file in $(grep -ril -E '\$\(CXXFLAGS\)' ./*); do
    sed 's/$(CXXFLAGS)/$(CXXFLAGS) $(EXTRA_CXXFLAGS)/' -i $file;
done

# Now, compile and install. Setting create_runpath_symlink variable to
# "no" is to disable creating /opt/Ice-$VERSION link to the
# installation directory.
make \
    EXTRA_CFLAGS="$SLKCFLAGS" \
    EXTRA_CXXFLAGS="$SLKCFLAGS"
make install \
    create_runpath_symlink="no" \
    prefix=$PKG/usr

# Installation rules in Ice makefiles are written for installing
# everything into completely separate tree (/opt/Ice-$VERSION by
# default) from the rest of the file-system.  Some of this is
# overcame by setting "prefix=$PKG/usr" when doing "make install"
# above, but we'll still have to move around some of installed files
# here.
rm $PKG/usr/ICE_LICENSE $PKG/usr/LICENSE
mkdir -p $PKG/usr/share/Ice
mv $PKG/usr/config $PKG/usr/share/Ice
mv $PKG/usr/slice $PKG/usr/share/Ice
if echo $ICE_LANGUAGES | grep -q py; then
    PYTHON_VERSION=$(python --version 2>&1 | sed 's/[^0-9]*\([0-9]\)\.\([0-9]\).*/\1.\2/')
    mkdir -p $PKG/usr/lib/python$PYTHON_VERSION/site-packages
    mv $PKG/usr/python/* $PKG/usr/lib/python$PYTHON_VERSION/site-packages
    rmdir $PKG/usr/python
fi
if echo $ICE_LANGUAGES | grep -q rb; then
    RUBY_VERSION=$(ruby --version | sed 's/[^0-9]*\([0-9]\)\.\([0-9]\).*/\1.\2/')
    mkdir -p $PKG/usr/lib/ruby/site_ruby/$RUBY_VERSION
    mv $PKG/usr/ruby/* $PKG/usr/lib/ruby/site_ruby/$RUBY_VERSION
    rmdir $PKG/usr/ruby
fi

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a CHANGES ICE_LICENSE LICENSE README RELEASE_NOTES \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
